package com.example.jetpack_databinding.main6

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.jetpack_databinding.R
import com.example.jetpack_databinding.databinding.RvItemBinding
import com.example.jetpack_databinding.main6.Rvadapter.MyHolder

class Rvadapter(var items: List<RvItem>) : RecyclerView.Adapter<MyHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val itemBinding: RvItemBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.rv_item,
            parent,
            false
        )
        return MyHolder(itemBinding)
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        val rvItem = items[position]
        holder.itemBinding?.rvItem = rvItem
    }

    override fun getItemCount(): Int {
        return items.size
    }

    class MyHolder : RecyclerView.ViewHolder {
        var itemBinding: RvItemBinding? = null

        constructor(itemBinding: RvItemBinding) : super(itemBinding.root) {
            this.itemBinding = itemBinding
        }
    }
}